﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Business;
using AMM.Model;
using AMM.Model.Business;

namespace AMM.Business.Business {
    public class ProduceConductProcessor : BaseProcessor {

        private int _pageSize;
        private ProduceConductRepository _ProduceConductRepository;

        public ProduceConductProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._ProduceConductRepository = new ProduceConductRepository(pageSize);
        }

        public ProduceConduct FetchProduceConductById(int produceConductId) {
            try {
                return this._ProduceConductRepository.FetchProduceConductById(produceConductId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定生产指令单失败！");
            }
        }

        public List<ProduceConduct> FetchProduceConductListById(int produceConductId) {
            try {
                ProduceConduct produceConduct = this._ProduceConductRepository.FetchProduceConductById(produceConductId);
                return this._ProduceConductRepository.FetchProduceConductByCustomerOrderId(produceConduct.CustomerOrderId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定生产指令单失败！");
            }
        }

        public List<ProduceConductViewModel> FetchProduceConductViewModelList(ProduceConductSearchModel searchModel, int pageIndex = 1) {
            try {
                List<ProduceConduct> resultList = this._ProduceConductRepository.FetchProduceConductList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new ProduceConductViewModel() {
                        Key = x.ProduceConductId,
                        BatchAmount = x.BatchAmount,
                        CustomerOrderCode = x.CustomerOrder.OrderNo,
                        ProductCode = x.Product.Code,
                        OrderDate = x.OrderDate,
                        ProduceConductCode = x.Code,
                        ProduceConductType = x.Basic.Code,
                        IsLocked = x.IsLocked,
                        ProcurementOrderCode = x.ProcurementOrder == null ? string.Empty : x.ProcurementOrder.Code,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取生产指令单列表失败！");
            }
        }

        public int GetTheMaxFlowNumber(string startString) {
            try {
                return this._ProduceConductRepository.GetTheMaxFlowNumber(startString) + 1;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取生产指令单最大流水号失败！");
            }
        }

        public void Update(List<ProduceConduct> produceConductList) {
            try {
                produceConductList.ForEach(x => {
                    if (x.ClassStatus == ClassStatus.Added) {
                        x.ProduceConductId = Guid.NewGuid().GetHashCode();
                    }
                    else {
                        ProduceConduct oldProduceConduct = this._ProduceConductRepository.FetchProduceConductById(x.ProduceConductId);
                        if (!oldProduceConduct.RowVersion.SequenceEqual(x.RowVersion)) {
                            throw this.BuildException("更新生产指令单失败，数据不一致性错误！");
                        }
                    }
                });
                this._ProduceConductRepository.UpdateProduceConduct(produceConductList);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("保存生产指令单失败！");
            }
        }

        public void Delete(ProduceConduct ProduceConduct) {
            try {
                this._ProduceConductRepository.Delete(ProduceConduct);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除生产指令单失败！");
            }
        }

        public void Update(ProduceConduct ProduceConduct) {
            try {
                this._ProduceConductRepository.Update(ProduceConduct);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新生产指令单失败！");
            }
        }

        public void Delete(params int[] ProduceConductIds) {
            try {
                this._ProduceConductRepository.DeleteMultipleProduceConduct(ProduceConductIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除生产指令单失败！");
            }
        }

        public List<OutStoreOrderDetail> FetchProduceConductMaterialList(int produceConductId) {
            try {
                List<OutStoreOrderDetail> resultList = this._ProduceConductRepository.FetchProduceConductMaterialList(produceConductId);
                resultList.ForEach(x => { if (x.Quantity > x.StoreQuantity) { x.ErrMessage = "库存不足"; } });
                return resultList;
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取生产指令单物料列表失败！");
            }
        }
    }
}
